package com.daguchuangyi.erp_android.productlist.classifiaction;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.R;
import com.jude.utils.JUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lijianjian on 2018/3/10.
 */

public class ClassificationTreeAdapter extends BaseQuickAdapter<ClassificationModel, BaseViewHolder> {

    private int mCurrentLevel = -1;
    private String mCurrentId = "";

    private Map<Integer, ClassificationModel> mCheckMap = new HashMap<>();

    public ClassificationTreeAdapter(int layoutResId, @Nullable List<ClassificationModel> data) {
        super(layoutResId, data);
    }

    public Map<Integer, ClassificationModel> getCheckedTree() {
        int size = mCheckMap.size();
        if (size - 1 > mCurrentLevel) {
            for (int i = size - 1; i > mCurrentLevel; i--) {
                mCheckMap.remove(i);
            }
        }
        return mCheckMap;
    }


    @Override
    protected void convert(BaseViewHolder helper, ClassificationModel item) {
        TextView titleTv = helper.getView(R.id.title_tv);
        ImageView nodeHintIv = helper.getView(R.id.node_hint_iv);
        TextView countTv = helper.getView(R.id.count_tv);

        titleTv.setText(item.getTitle());
        countTv.setText(String.valueOf(item.getCount()));

        int leftMargin = JUtils.dip2px(24);
        int level = item.getLevel();
        leftMargin += level * leftMargin;
        ((ViewGroup.MarginLayoutParams) helper.getView(R.id.title_tv).getLayoutParams()).leftMargin = leftMargin;


        helper.itemView.setOnClickListener(v -> {
            for (int i = 0; i < getData().size(); i++) {
                ClassificationModel treeItem = getData().get(i);
                if (treeItem.getLevel() == item.getLevel()) {
                    if (!TextUtils.equals(treeItem.getId(), item.getId()) && treeItem.isExpanded()) {
                        int size = collapse(i);
                        for (ClassificationModel subSubItem : treeItem.getSubItems()) {
                            // collapse all sub item
                            subSubItem.setExpanded(false);
                        }
                    }
                }
            }
            final int pos = helper.getAdapterPosition();
            Log.i(TAG, "new handle item : " + pos);

            mCurrentId = item.getId();
            mCurrentLevel = item.getLevel();

            if (item.isExpanded()) {
                collapse(pos);
            } else {
                expand(pos);
            }
            notifyDataSetChanged();
        });

        boolean isExpanded = item.isExpanded();
        int highLightColor = Color.parseColor("#64a2f0");
        if (TextUtils.equals(mCurrentId, item.getId()) && item.getLevel() == mCurrentLevel) {
            JUtils.Log("set checked style.");
            helper.itemView.setBackgroundColor(Color.parseColor("#f8f8f9"));
            titleTv.setTextColor(highLightColor);
            countTv.setTextColor(Color.parseColor("#8064a2f0"));
            mCheckMap.put(item.getLevel(), item);
            if (isExpanded) {
                nodeHintIv.setImageResource(R.drawable.ic_arrow_up_check);
            } else {
                nodeHintIv.setImageResource(R.drawable.ic_arrow_down);
            }
        } else {
            int normalColor = Color.parseColor("#4c071c4e");
            helper.itemView.setBackgroundColor(Color.WHITE);
            countTv.setTextColor(normalColor);
            if (isExpanded) {
                mCheckMap.put(item.getLevel(), item);
                titleTv.setTextColor(highLightColor);
                nodeHintIv.setImageResource(R.drawable.ic_arrow_up_normal);
            } else {
                titleTv.setTextColor(Color.parseColor("#4e5565"));
                nodeHintIv.setImageResource(R.drawable.ic_arrow_down);
            }
        }

        if (item.hasSubItem()) {
            helper.getView(R.id.node_hint_iv).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.node_hint_iv).setVisibility(View.INVISIBLE);
        }


    }
}
